﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Autodesk.Revit.DB;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	internal static class Log
	{
		public static bool Enabled { get; set; }

		static Log()
		{
			Directory.CreateDirectory(Path.GetDirectoryName(Log.LogFile));
			Log.Enabled = false;
		}

		private static void WriteLog(IEnumerable<string> msgs)
		{
			if (!Log.Enabled)
			{
				return;
			}
			StreamWriter sw = File.Exists(Log.LogFile) ? File.AppendText(Log.LogFile) : File.CreateText(Log.LogFile);
			msgs.ForEach(delegate(string s)
			{
				sw.WriteLine(s);
			});
			sw.Close();
		}

		private static string FormatMessage(string msgType, string msg)
		{
			return string.Format("{0}\t{1}\t\t{2}", DateTime.Now.ToString("hh:mm:ss"), msgType, msg);
		}

		internal static void Info(params string[] msgs)
		{
			Log.Info(msgs.AsEnumerable<string>());
		}

		internal static void Info(IEnumerable<string> msgs)
		{
			Log.WriteLog(msgs.ForEach((string s) => Log.FormatMessage("Info", s)));
		}

		internal static void Warning(params string[] msgs)
		{
			Log.Info(msgs.AsEnumerable<string>());
		}

		internal static void Warning(IEnumerable<string> msgs)
		{
			Log.WriteLog(msgs.ForEach((string s) => Log.FormatMessage("Warning", s)));
		}

		internal static void Error(params string[] msgs)
		{
			Log.Info(msgs.AsEnumerable<string>());
		}

		internal static void Error(IEnumerable<string> msgs)
		{
			Log.WriteLog(msgs.ForEach((string s) => Log.FormatMessage("Error", s)));
		}

		internal static void Element(Element element)
		{
			Log.Info(new string[]
			{
				string.Format("Element Name = {0} , Type =  {1}, Id = {2}", element.Name, element.GetType(), element.Id.IntegerValue)
			});
		}

		internal static void HYPipe(HyCalcPipe hyPipe)
		{
			Log.Info(new string[]
			{
				string.Format("Pipe Name = {0} Id = {1} Flow = {2} DN = {3} Length = {4} Veocity = {5} Resistacne = {6} ResistanceJ = {7} ResistanceY = {8}", new object[]
				{
					hyPipe.Name,
					hyPipe.EndNode.Id.IntegerValue,
					hyPipe.Flow,
					hyPipe.DN,
					hyPipe.Length,
					hyPipe.Velocity,
					hyPipe.Resistance,
					hyPipe.Resistancej,
					hyPipe.Resistancey
				})
			});
		}

		internal static void XYZ(XYZ point)
		{
			Log.Info(new string[]
			{
				string.Format("Point  = [{0} , {1},  {2}]", point.X, point.Y, point.Z)
			});
		}

		private static readonly string LogFile = Path.Combine(Product.WorkingLocation, string.Format("log\\{0}\\{1}.log", Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location), DateTime.Now.ToString("yyyy-MM-dd")));
	}
}
